<template>
  <div class="closing">
    <div>
      <el-button size="mini" @click="handleGetUntreated">{{ $t('btn.reload') }}</el-button>
      <el-button type="primary" size="mini" @click="isClosingDialog=true">{{ $t('market.filter') }}<!--过滤--></el-button>
      <el-button type="primary" size="mini" @click="handleExport">{{ $t('btn.toExcel') }}</el-button>
    </div>
    <div class="header">
      <div class="header-name">
        <span>{{ $t('market.fdate') }}<!--期间--></span>
        <el-input v-model="period" size="mini" disabled />
      </div>
    </div>
    <div class="table-content">
      <jc-table
        v-loading="loading"
        :table-data="tableData"
        :table-header="tableHeader"
        :table-height="tableHeight"
        serial
        sortable
      >
        <template v-slot:billSlot="col">
          <el-link type="primary" @click="pushView(col.scope.row, col.scope.$index)">{{ col.scope.row.cashFlow }}</el-link>
        </template>
      </jc-table>
    </div>
    <!-- 过滤 -->
    <el-dialog
      v-dialogDrag
      :title="$t('market.filter')"
      model
      :visible.sync="isClosingDialog"
      :close-on-click-modal="false"
      width="400px"
      center
    >
      <div>
        <el-form :model="subDetails" size="mini" label-width="100px">
          <el-form-item :label="$t('market.fdate')">
            <el-date-picker
              v-model="subDetails.start"
              type="month"
              value-format="yyyy-MM"
            />
          </el-form-item>
        </el-form>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="isClosingDialog = false">{{ $t('btn.cancel') }}</el-button>
        <el-button type="primary" @click="initialCon">{{ $t('btn.confirm') }}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  mainCashFlowSheetList
} from '@/api/financial/subject'
import { export_json_to_excel, formatJson } from '@/utils/Export2Excel'

export default {
  name: 'Closing',
  data() {
    return {
      subDetails: {
        fdate: ''
      },
      expandKeys: [], // 展开行
      toExcelData: [],
      period: '',
      loading: false,
      isClosingDialog: false,
      // 表头
      tableHeight: '70vh',
      tableData: [],
      tableHeader: [
        { label: this.$t('market.cashFlow'), type: 'bill', prop: 'cashFlow', width: '400' },
        { label: this.$t('market.currentAmount1'), prop: 'currentAmount', width: '150' },
        { label: this.$t('market.proportion1'), prop: 'lastAmount', width: '150' },
        { label: this.$t('market.yearAmount1'), prop: 'yearAmount', width: '150' }
      ],
      start: '',
      end: ''
    }
  },
  created() {
    this.tableHeight = window.innerWidth > 1440 ? 'calc(100vh - 200px)' : '73vh'
  },
  methods: {
    // 过滤确认
    initialCon() {
      this.isClosingDialog = false
      this.period = this.subDetails.start
      this.handleGetUntreated()
    },
    // 获取科目余额列表数据
    async handleGetUntreated() {
      if (!this.period) {
        this.isClosingDialog = true
        return
      }
      const DATA = {
        fdate: this.period
      }
      const RES = await mainCashFlowSheetList(DATA)
      if (RES.code === 0) {
        this.tableData = RES.data
      } else {
        this.$confirm(RES.message, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'error'
        })
      }
    },
    // 搜索
    handleQuerySonClass() {
      this.currentPage = 1
      this.handleGetUntreated()
    },
    pushView(ev, index) {
      this.$router.push({
        path: `/flowSheetsDetail/${index}`,
        query: {
          fdate: this.period,
          cashFlow: ev.cashFlow
        }
      })
    },
    // 导出表格
    handleExport() {
      require.ensure([], () => {
        const tHeader = this.tableHeader.map((res) => {
          return res.label
        })
        const filterVal = this.tableHeader.map((res) => {
          return res.prop
        })
        if (this.tableData.length <= 0) {
          this.$message.warning('没有导出数据')
        } else {
          const list = this.tableData
          const data = formatJson(filterVal, list)
          export_json_to_excel(tHeader, data, '现金流量表')
        }
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.closing {
  @include listBom;
  .el-input::v-deep .el-input__inner {
    height: 30px;
    line-height: 30px;
  }
  .header{
    margin-top: 10px;
    .header-name{
      span {
        width: 100px;
        margin-right: 10px;
        text-align: right;
      }
    }
  }
}
</style>
